package com.his.organization.service.Imp;

import com.github.pagehelper.util.StringUtil;
import com.his.common.util.Utils;
import com.his.login.entity.DictEmployee;
import com.his.organization.dao.DictOrganizationMapper;
import com.his.organization.entity.DictOrganization;
import com.his.organization.service.OrganizationService;
import com.his.organization.vo.OrganizationVo;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.PinYin4jUtil;
import com.xiao.common.util.WorkIdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xiaoxiaoping
 * @Date 2020/2/6 15:28
 * @Version 1.0
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Resource
    DictOrganizationMapper dictOrganizationMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DictEmployee dictEmployee,OrganizationVo organizationVo) {
        DictOrganization dictOrganization = BeanUtil.copyBean(organizationVo,DictOrganization.class);
        Map params = new HashMap();
        String hospid;
        //判断实体里有hospid没 没有就去操作者的hospid
        if(StringUtils.isEmpty(organizationVo.getHOSPID())){
            params.put("hospid",dictEmployee.getHOSPID());
            hospid = dictEmployee.getHOSPID();
        }else{
            params.put("hospid",organizationVo.getHOSPID());
            hospid = organizationVo.getHOSPID();
        }
        String orgid;
        if(StringUtil.isEmpty(organizationVo.getORGID())) {
            params.put("orgid", StringUtils.isEmpty(organizationVo.getUPID()) ? dictEmployee.getHOSPID() : organizationVo.getUPID());
            orgid = dictOrganizationMapper.selectOrgId(params);
        }else{
            orgid = organizationVo.getORGID();
        }
        dictOrganization.setORGID(orgid);
        dictOrganization.setINPUTSTR(PinYin4jUtil.converterToFirstSpell(organizationVo.getORGNAME()));
        dictOrganization.setONLINEFLAG(1);
        dictOrganization.setHOSPID(hospid);
        dictOrganization.setUPID(StringUtils.isEmpty(organizationVo.getUPID()) ? dictEmployee.getHOSPID() : organizationVo.getUPID());
        if(null == organizationVo.getATTRIBUTE()){
            dictOrganization.setATTRIBUTE(2);
        }
        dictOrganizationMapper.insertAllColumn(dictOrganization);
    }

    @Override
    public void update(OrganizationVo organizationVo) {
        DictOrganization dictOrganization = BeanUtil.copyBean(organizationVo,DictOrganization.class);
        dictOrganizationMapper.updateById(dictOrganization);
    }

    @Override
    public DictOrganization selectById(String orgId) {
        return dictOrganizationMapper.selectById(orgId);
    }

    @Override
    public List<DictOrganization> selectByEntity(DictEmployee dictEmployee, OrganizationVo organizationVo) {
        DictOrganization dictOrganization = BeanUtil.copyBean(organizationVo,DictOrganization.class);
        String hospId;
        if(StringUtils.isEmpty(organizationVo.getHOSPID())){
            hospId = dictEmployee.getHOSPID();
        }else{
            hospId = organizationVo.getHOSPID();
        }
        dictOrganization.setHOSPID(hospId);
        dictOrganization.setONLINEFLAG(1);
        return dictOrganizationMapper.selectByEntity(dictOrganization);
    }

    @Override
    public List<Map<String, Object>> getOrgTree(DictEmployee dictEmployee) {
        DictOrganization dictOrganization = new DictOrganization();
        dictOrganization.setHOSPID(dictEmployee.getHOSPID());
        List<DictOrganization> list = dictOrganizationMapper.selectByEntity(dictOrganization);
        List<Map<String, Object>> menuList = new ArrayList<>();
        if(list.size()>0){
            //一级菜单
            for(DictOrganization dictOrganization1 : list){
                menuList.add(toNode(dictOrganization1));
            }
            //list转成tree
            return Utils.getTree(menuList,null,"id");
        }
        return null;
    }


    private Map<String ,Object> toNode(DictOrganization dictOrganization){
        Map<String, Object> node = new HashMap<>();
        node.put("id",dictOrganization.getORGID());
        node.put("title",dictOrganization.getORGNAME());
        node.put("grade",dictOrganization.getGRADE());
        node.put("pid", dictOrganization.getUPID());
        return node;
    }
}
